fix: composite primary key tables update/delete wrong rows#742
Merged
fix: composite primary key tables update/delete wrong rows#742
Conversation
Thread primaryKeyColumns: [String] through the entire editing pipeline, replacing primaryKeyColumn: String? which silently dropped all but the first PK column. UPDATE and DELETE now use all PK columns in WHERE. Also: structure view saves now respect safe mode on read-only connections, and discardChanges() clears stale changedRowIndices.
The .onChange handler for column changes unconditionally set primaryKeyColumns to [firstColumn], ignoring the actual schema PKs. Now reads from tab.primaryKeyColumns which preserves the full composite PK set from schema detection.
PRAGMA table_info returns pk as position (1, 2, 3...) not boolean. The check row[5] == "1" only matched the first PK column. Changed to row[5] != "0" to detect all columns in a composite primary key.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes editing in tables with composite primary keys. Previously,
UPDATEandDELETEstatements only used the first PK column in theWHEREclause, causing all rows sharing that value to be affected instead of just the target row.Root cause: The entire editing pipeline modeled primary keys as
primaryKeyColumn: String?(single column). Schema detection used.first(where: { $0.isPrimaryKey })which silently dropped all but the first PK column.Changes
Composite PK support (15 source files)
Thread
primaryKeyColumns: [String]through the pipeline, replacingprimaryKeyColumn: String?:TableSchema—primaryKeyColumns: [String], computedprimaryKeyColumnfor backward compatQueryTab,TabPendingChanges— same renameDataChangeManager—primaryKeyColumns: [String]+configureForTablesignatureSQLStatementGenerator— UPDATE: loops all PK columns →WHERE a = ? AND b = ?; batch DELETE:(a = ? AND b = ?) OR (a = ? AND b = ?)MainContentCoordinator+QueryHelpers—.filter { $0.isPrimaryKey }.map(\.name)instead of.first(where:)?.nameDataGridView,DataGridCoordinator—primaryKeyColumns: [String]with computedprimaryKeyColumnRowOperationsManager— sets__DEFAULT__on all PK columns when duplicating rowsStructure view safe mode bypass (1 file)
TableStructureView+Schema.swift—executeSchemaChanges()now checksconnection.safeModeLevel.blocksAllWritesbefore executing DDLdiscardChanges()stale state (1 file)DataChangeManager.swift— added missingchangedRowIndices.removeAll()Tests (14 files)
SQLStatementGeneratorCompositePKTests.swift— 22 test casesprimaryKeyColumns: [String]parameter renameTest coverage (22 new tests)
$N, MSSQL[bracket]How to reproduce the original bug
Edit
quantityof row(1, 200)→ Save → Before fix: all 3 rows updated. After fix: only(1, 200)updated.Test plan